package com.koron.order.risk.base.excel;

import cn.hutool.core.map.BiMap;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.koron.common.core.business.datadict.utils.DataDictTool;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author jzp
 * @since 2023/1/10
 */
public class RiskStringStringConvertor extends StringStringConverter {
    /**
     * {
     *     字段名：{
     *         数据库值：前端显示值
     *     }
     * }
     */
    private final Map<String, BiMap<String, String>> fieldMap = new HashMap<>();
    private final String title = "$title$";

    public RiskStringStringConvertor() {
        // 风险等级 risk_level
        BiMap<String, String> riskLevelMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_level"));
        riskLevelMap.put(title, "风险等级");

        // 对线路的影响 risk_influence_for_line
        BiMap<String, String> influenceForLineMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_influence_for_line"));
        influenceForLineMap.put(title, "对线路的影响");

        // 处理方式 risk_handler_way
        BiMap<String, String> handlerWayMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_handler_way"));
        handlerWayMap.put(title, "处理方式");

        // 风险来源 risk_source
        BiMap<String, String> riskSourceMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_source"));
        riskSourceMap.put(title, "风险来源");

        // 建筑物类型 buildingType
        BiMap<String, String> buildingTypeMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "building_type"));
        buildingTypeMap.put(title, "建筑物类型");


        // 字段和数据字典对应
        fieldMap.put("riskLevel", riskLevelMap);
        fieldMap.put("influenceForLine", influenceForLineMap);
        fieldMap.put("handlerWay", handlerWayMap);
        fieldMap.put("riskSource", riskSourceMap);
        fieldMap.put("buildingType", buildingTypeMap);

    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String key = cellData.getStringValue();
        if (contentProperty == null) {
            return key;
        }
        ExcelProperty excelProperty = contentProperty.getField().getAnnotation(ExcelProperty.class);
        if (excelProperty == null || excelProperty.converter() == AutoConverter.class) {
            return key;
        }
        BiMap<String, String> entries = fieldMap.get(contentProperty.getField().getName());
        if (entries != null) {
            key = entries.getKey(cellData.getStringValue());
            if (StrUtil.isNotEmpty(cellData.getStringValue()) && StrUtil.isEmpty(key)) {
                return cellData.getStringValue();
            }
        }
        return key;
    }

    @Override
    public WriteCellData<String> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (contentProperty == null) {
            return new WriteCellData<>(value);
        }
        ExcelProperty excelProperty = contentProperty.getField().getAnnotation(ExcelProperty.class);
        if (excelProperty.converter() != null && excelProperty.converter() != AutoConverter.class) {
            BiMap<String, String> entries = fieldMap.get(contentProperty.getField().getName());
            if (entries != null) {
                return new WriteCellData<>(entries.getOrDefault(Objects.toString(value, ""), ""));
            }
        }
        return new WriteCellData<>(value);
    }

}
